package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.vo.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/menu")
public class SysMenuController {
    @Resource
    SysMenuService sysMenuService;
    /*
        前端给角色分配菜单列表时
            1、需要可以用的父子菜单列表
            2、需要 当前角色已分配的菜单id的集合

     */
    @Operation(summary = "查询可用的菜单列表并组装为父子集合和角色已分配的菜单id集合")
    @GetMapping("role/listMenus/{roleId}")
    public Result<Map<String , Object>> listRoleMenus(@PathVariable("roleId")Long roleId){
        Map<String , Object>  map = sysMenuService.listRoleMenus(roleId);
        return Result.ok(map);
    }

    //如果它有下一级菜单不能直接删除
    @Operation(summary = "删除指定id的菜单")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id") Long id){
        sysMenuService.deleteById(id);
        return Result.ok(null);
    }
    @Operation(summary = "根据id查询菜单")
    @GetMapping("{id}")
    public Result<SysMenu> getById(@PathVariable("id") Long id){
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok(sysMenu);
    }
    @Operation(summary = "根据id更新菜单")
    @PutMapping()
    public Result<Object> updateById(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return Result.ok(null);
    }
    //因为菜单之间有级联关系：必须指定菜单的parentId
    @Operation(summary = "新增菜单")
    @PostMapping()
    public Result<Object> save(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return Result.ok(null);
    }
    @Operation(summary = "查询所有菜单和它的子菜单")
    @GetMapping("listMenus")
    public Result<List<SysMenu>> listMenus(){
        //查询组装好的父子菜单集合返回:  查询到父菜单集合，将每个父菜单的子菜单集合设置给他
        return Result.ok(sysMenuService.listMenus());
    }
}
